package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.AdminRoleButton;
import com.dingreading.cloud.admin.entity.table.AdminRoleButtonTableDef;
import com.dingreading.cloud.admin.mapper.AdminRoleButtonMapper;
import com.dingreading.cloud.admin.service.AdminRoleButtonService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class AdminRoleButtonServiceImpl extends BaseServiceImpl<AdminRoleButtonMapper, AdminRoleButton, AdminRoleButtonTableDef> implements AdminRoleButtonService {

    @Override
    protected AdminRoleButtonTableDef getTable() {
        return AdminRoleButtonTableDef.adminRoleButton;
    }

    @Resource
    AdminRoleButtonMapper roleButtonMapper;

    @Override
    public void dropByButtonId(Long id) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(table.buttonId.eq(id));
        roleButtonMapper.deleteByQuery(queryWrapper);
    }

    @Override
    public Page<AdminRoleButton> pageList(PageUtil pageUtil, Long roleId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .from(table)
                .where(table.roleId.like(roleId))
                .orderBy(table.id.desc());

        return roleButtonMapper.paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), queryWrapper);
    }

    @Override
    public long getByButtonId(Long id) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(table.id)
                .from(table)
                .where(table.buttonId.eq(id));
        return getMapper().selectCountByQuery(queryWrapper);
    }

    @Override
    public int dropBean(Long id) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(table.id.eq(id));
        return getMapper().deleteByQuery(queryWrapper);
    }

    @Override
    public List<Object> getByRoleIdAndButtonIds(Long roleId, List<Long> buttonIds) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(table.buttonId)
                .from(table)
                .where(table.buttonId.in(buttonIds)
                        .and(table.roleId.eq(roleId)));
        return getMapper().selectObjectListByQuery(queryWrapper);
    }
}
